<?php
//通过trait水平扩展php类功能
trait Power
{
    protected $power;

    protected function gas()
    {
        // return '汽油';
        $this->power = '汽油';
    }

    protected function battery()
    {
        // return '电池';
        $this->power = '电池';
    }

    public function water()
    {
        return '水';
    }

    public function print()
    {
        echo "动力来源：" . $this->power . '<br/>';
    }
}

trait Engine
{
    protected $engine;

    protected function three()
    {
        // return '三缸发动机';
        $this->engine = 3;
    }

    protected function four()
    {
        // return '四缸发动机';
        $this->engine = 4;

    }

    public function print()
    {
        echo "发动机个数：" . $this->engine . '<br/>';
    }
}

abstract class BaseCar
{
    abstract public function drive();
    protected function gas()
    {
        echo "动力来源：柴油" . '<br/>';
    }
    abstract function battery();

    
}

trait Component
{
    use Power, Engine{
        Engine::print insteadof Power;
        Power::print as printPower;
        Engine::print as printEngine;
    }

    protected function init()
    {
        $this->gas();
        $this->four();
    }
}

class Car extends BaseCar
{
    use Component;

    public function drive()
    {
        
        // echo "动力来源：" . $this->water() . '<br/>';
        // echo "切换动力来源：" . $this->battery() . '<br/>';
        // echo "切换动力来源：" . $this->gas() . '<br/>';
        // echo "动力来源:" . $this->power . '<br/>';
        // echo "动力来源:" . $this->gas() . '<br/>';
        // echo "发动机：" . $this->four() . '<br/>'; 
        $this->init();
        // $this->print();
        $this->printPower();
        $this->printEngine();
        echo "汽车启动...";

    }

 
}

$car = new Car();
$car->drive();